package com.xuxin.baby.model;

import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.contract.ChildDataRecordContract;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.network.RetrofitApi;
import com.xuxin.baby.network.myObserver;
import com.xuxin.baby.presenter.ChildDataRecordPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2020/10/22 0022
 * 记身高
 */
public class ChildDataRecordModel implements ChildDataRecordContract.Model {
    @Override
    public void record(ChildDataRecordPresenter presenter,
                       String tenantId,
                       String token,
                       String eventDate,
                       String childId,
                       float height,
                       float weight,
                       float headCircle,
                       int sleepHours,
                       int sleepMinutes,
                       String eventType) {
        try {
            JSONObject object = new JSONObject();
            object.put("eventDate", eventDate);
            object.put("childId", childId);
            object.put("height", height);
            object.put("weight", weight);
            object.put("headCirc", headCircle);
            object.put("sleepHours", sleepHours);
            object.put("sleepMinutes", sleepMinutes);
            object.put("eventType", eventType);
            RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .recordChildData(tenantId,
                            token,"1",
                            requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new myObserver<BaseBean>() {
                        @Override
                        public void onNextX(@NonNull BaseBean baseBean) {
                            if (baseBean.isSuccess()) {
                                presenter.onSuccess(baseBean);
                            } else {
                                presenter.onError(baseBean);
                            }
                        }
                        @Override
                        public void onError(BaseBean baseBean) {
                            presenter.onError(baseBean);
                        }
                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                            presenter.onComplete();
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}